<?php
class CommonLogicService {
	public $dbutil;
	function __construct($dbutil) {
		$this->dbutil = $dbutil;
	}
	
	public function nordataPage($table, $start, $page_size) {
		$limit = "";
		if ($page_size) {
			$limit = " limit $start,$page_size ";
		}
		$sql = "select * from $table $where $sort  $limit ";
		return $this->dbutil->get_results ( $sql );
	}
	
	public function dataPage($table, $start, $page_size, $sort,$where) {
		$limit = "";
		if ($page_size) {
			$limit = " limit $start,$page_size ";
		}
		if($where){
			$where = "where ".$where;
		}
		$sql = "select * from $table $where $sort  $limit ";
		return $this->dbutil->get_results ( $sql );
	}
	public function dataPageWhere($table, $start, $page_size, $where) {
		$limit = "";
		if ($page_size) {
			$limit = " limit $start,$page_size ";
		}
		
		$sql = "select * from $table $where   $limit";
		return $this->dbutil->get_results ( $sql );
	}
	public function countNum($table) {
		$sql = "select count(*) countnum  from $table";
		$resutrnarry = $this->dbutil->get_results ( $sql );
		return $resutrnarry [0]->countnum;
	}
	public function countNumWhere($table, $where) {
		$sql = "select count(*) countnum  from $table";
		if ($where != '') {
			$sql .= " $where";
		}
		$resutrnarry = $this->dbutil->get_results ( $sql );
		return $resutrnarry [0]->countnum;
	}
	public function insert($table, $data) {
		$this->dbutil->insert ( $table, $data );
	}
	public function update($table, $data, $where) {
		$this->dbutil->update ( $table, $data, $where );
	}
	public function del($sql) {
		$this->dbutil->query ( $sql );
	}
	public function executeSql($sql) {
		return $this->dbutil->query ( $sql );
	}
	public function getResults($sql) {
		return $this->dbutil->get_results ( $sql );
	}
	public function getResult($sql) {
		return $this->dbutil->get_row ( $sql );
	}
	public function insertItem($table, $column, $name) {
		$sql = "insert into " . $table . " (" . $column . ") values('" . $name . "')";
		if (self::countTableNum ( $table, $column, $name ) == 0) {
			$this->dbutil->get_results ( $sql );
			return 0;
		} else {
			return 1;
		}
	}
	public function countTableNum($table, $column, $where) {
		$sql = "select count(" . $column . ") countnum from " . $table . " where " . $column . " = '" . $where . "'";
		$resutrnarry = $this->dbutil->get_results ( $sql );
		return $resutrnarry [0]->countnum;
	}
}